package com.app.business.web.business;

import com.app.business.en.HouseStatus;
import com.app.business.en.Status;
import com.app.business.model.*;
import com.app.business.service.*;
import com.app.business.utils.ChooseLimitUtils;
import com.app.business.utils.SetHouseStyleUtils;
import com.app.system.jpa.PageBean;
import com.app.system.permission.model.DataDict;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.app.system.utils.dataType.DateUtils;
import com.app.system.utils.dataType.IntegerUtils;
import com.app.system.utils.dataType.StringUtils;
import com.app.system.utils.exception.Precondition;
import com.app.system.utils.exception.Response;
import com.app.system.utils.page.LayUiUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 选房业务办理
 * Created by ZYK on 2019/2/13.
 */
@Controller
@RequestMapping("/business/chooseHouse/*")
public class ChooseHouseController {

    @Autowired
    private ContractService contractService;
    @Autowired
    private DataDictService dataDictService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private ShopHouseService shopHouseService;
    @Autowired
    private AzProjectService azProjectService;
    @Autowired
    private StorageRoomService storageRoomService;
    @Autowired
    private CarLotService carLotService;
    @Autowired
    private ActionHistoryService actionHistoryService;
    @Autowired
    private ParameterService parameterService;

    /**
     * 基础信息数据列表
     */
    @RequestMapping("list")
    @RequiresPermissions("chooseHouse:list")
    public void list() {
    }

    /**
     * 加载基础信息数据
     *
     * @return
     */
    @RequestMapping("loadContractList")
    @ResponseBody
    @RequiresPermissions("chooseHouse:loadContractList")
    public Map loadContractList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<Contract> contractPageBean = this.contractService.getContractPageBean();
        List<Contract> contractList = contractPageBean.getDatas();
        Map<String, DataDict> signMap = this.dataDictService.getMapByParentNameForValue("签约状态");
        Map<String, DataDict> townMap = this.dataDictService.getMapByParentNameForValue("乡镇");
        Map<String, DataDict> checkMap = this.dataDictService.getMapByParentNameForValue("复核状态");
        Map<String, DataDict> chooseHouseMap = this.dataDictService.getMapByParentNameForValue("房源签认状态");
        if (!CollectionUtils.isEmpty(contractList)) {
            Map<String, Object> item = null;
            for (Contract contract : contractList) {
                item = null;
                item = Maps.newHashMap();
                item.put("id", contract.getId());
                item.put("town", StringUtils.dataDictShow(townMap.get(contract.getZsProject().getTown())));
                item.put("zsProjectName", contract.getZsProject().getProjectName());
                item.put("contractCode", StringUtils.getNotNull(contract.getContractCode()));
                item.put("name", StringUtils.getNotNull(contract.getName()));
                item.put("chooseHouseNum", StringUtils.getNotNull(contract.getChooseHouseNum()));
                item.put("limitHouseAreaTotal", BigDecimalUtils.getNotNullStr(contract.getLimitHouseAreaTotal()));
                item.put("chooseHouseStatus", IntegerUtils.getNotNull(contract.getChooseHouseStatus()));
                item.put("chooseHouseStatusStr", StringUtils.dataDictShow(chooseHouseMap.get(IntegerUtils.getNotNull(contract.getChooseHouseStatus()).toString())));
                item.put("financeCheckStatus", IntegerUtils.getNotNull(contract.getFinanceCheckStatus()));
                item.put("financeCheckStatusStr", StringUtils.dataDictShow(checkMap.get(IntegerUtils.getNotNull(contract.getFinanceCheckStatus()).toString())));
                item.put("azSignStatusStr", StringUtils.dataDictShow(signMap.get(IntegerUtils.getNotNull(contract.getAzSignStatus()).toString())));
                item.put("chooseHouseDate", contract.getChooseHouseStatus().equals(Status.NO.getIndex()) ? "" : DateUtils.dateToStringDefault(contract.getChooseHouseDate()));
                listData.add(item);
            }
        }
        return LayUiUtils.page(contractPageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 选房界面
     */
    @RequestMapping("chooseHouse")
    @RequiresPermissions("chooseHouse:chooseHouse")
    public void chooseHouse(HttpServletRequest request, ModelMap modelMap) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkSqdsArguement(contract != null && contract.getZsProject() != null, "数据错误，请重试！");
        //通过征收项目id获取所分配的安置项目id
        List<String> azProjectIdList = this.houseService.getAzProjectIdListByZsProjectId(contract.getZsProject().getId());
        Map<String, AzProject> azProjectMap = this.azProjectService.getAzProjectMap();
        modelMap.addAttribute("contract", contract);
        modelMap.addAttribute("azProjectIdList", azProjectIdList);
        modelMap.addAttribute("azProjectMap", azProjectMap);
    }


    /**
     * 打印时验证该户是否选择大套型
     * 该户是否需要进行大户型限制
     *
     * @param request
     * @return
     */
    @RequestMapping("getIsMustBigHouse")
    @ResponseBody
    public Boolean getIsMustBigHouse(HttpServletRequest request) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkSqdsArguement(contract != null && contract.getZsProject() != null, "数据错误，请重试！");
        Boolean isMustBigHouse = true;
        if (contract.getIsChooseBigHouse() != null && contract.getIsChooseBigHouse() == 1) {
            isMustBigHouse = false;
            List<House> houseList = houseService.getHouseListByContractId(contract.getId());
            if (CollectionUtils.isNotEmpty(houseList)) {
                for (House house : houseList) {
                    if (house.getBuildArea().compareTo(new BigDecimal("170")) > 0) {
                        isMustBigHouse = true;
                    }
                }
            }
        }
        return isMustBigHouse;
    }


    /**
     * 楼盘图界面
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("housePicture")
    @RequiresPermissions("chooseHouse:housePicture")
    public void housePicture(HttpServletRequest request, ModelMap modelMap) {
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        AzProject azProject = this.azProjectService.findOne(azProjectId);
        Precondition.checkSqdsArguement(azProject != null && contract != null, "数据错误！");
        List<String> buildNumList = this.houseService.getBuildNumListByAzProjectId(contract.getZsProject().getId(), azProject.getId());
        Map<String, String> colorMap = this.dataDictService.getMapByParentNameForValue_Color("公示颜色");

        modelMap.addAttribute("colorMap", colorMap);
        modelMap.addAttribute("azProject", azProject);
        modelMap.addAttribute("contract", contract);
        modelMap.addAttribute("buildNumList", buildNumList);
    }

    //加载楼板盘
    @RequestMapping("loadHousePicture")
    @ResponseBody
    @RequiresPermissions("chooseHouse:loadHousePicture")
    public Map loadHousePicture(HttpServletRequest request) {
        Integer zsProjectId = ParamUtils.getInt(request, "zsProjectId", 0);
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        String buildNum = ParamUtils.getString(request, "buildNum", "");
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkSqdsArguement(contract != null, "数据错误！");

        //通过征收项目、安置项目、楼号得到所分配的houseList
        List<House> houseList = houseService.houseList(zsProjectId.toString(), azProjectId, buildNum);
        //获取某户当前不可选房源IdList
        List<Integer> houseIdList = ChooseLimitUtils.getCountChooseHouseIdListByHouseStyle(contract, azProjectId);
        //得到层号和当前楼层的List<House>对应的map
        Map<String, List<House>> floorHouseMap = houseService.getFloorHouseForHouseStatusMap(houseList, houseIdList, contract);

        Map<String, Integer> maxFloorMap = houseService.getMaxFloorMap(zsProjectId.toString(), azProjectId, buildNum);
        Integer maxFloor = maxFloorMap.get("maxFloor");
        //房源状态
        Map<String, String> colorMap = this.dataDictService.getMapByParentNameForValue_Color("公示颜色");
        //房屋类型
        Map<String, String> houseHoldMap = this.dataDictService.getMapByParentNameForValue_Name("房屋户型");
        Map<String, Object> map = Maps.newHashMap();
        map.put("maxFloor", maxFloor);
        map.put("floorHouseMap", floorHouseMap);
        map.put("colorMap", colorMap);
        map.put("houseHoldMap", houseHoldMap);

        String limitMemo = ChooseLimitUtils.getChooseLimitForHosuePic(contract);
        map.put("limitMemo", limitMemo);
        map.put("chooseLimitType", contract.getChooseLimitType());
        return map;
    }

    /**
     * 加载房源基本信息
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("loadHouse")
    @RequiresPermissions("chooseHouse:loadHouse")
    public void loadHouse(HttpServletRequest request, ModelMap modelMap) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Integer houseId = ParamUtils.getInt(request, "houseId", 0);
        House house = this.houseService.findOne(houseId);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkSqdsArguement(house != null && contract != null, "数据错误！");
        Map<String, DataDict> houseTypeMap = this.dataDictService.getMapByParentNameForValue("房屋类型");
        Map<String, DataDict> houseHoldMap = this.dataDictService.getMapByParentNameForValue("房屋户型");
        modelMap.addAttribute("contract", contract);
        modelMap.addAttribute("house", house);
        modelMap.addAttribute("houseTypeMap", houseTypeMap);
        modelMap.addAttribute("houseHoldMap", houseHoldMap);
    }

    /**
     * 已选房源信息
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("selectHouseInfo")
    @RequiresPermissions("chooseHouse:selectHouseInfo")
    public void selectHouseInfo(HttpServletRequest request, ModelMap modelMap) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkSqdsArguement(contract != null, "数据错误，请重试！");
        Map<String, DataDict> houseTypeMap = this.dataDictService.getMapByParentNameForValue("房屋类型");
        Map<String, DataDict> houseHoldMap = this.dataDictService.getMapByParentNameForValue("房屋户型");
        List<House> houseList = this.houseService.getHouseListByContractId(contract.getId());
//        List<House> houseList = this.houseService.getHouseListByContractIdForArea(contract.getId());
        List<StorageRoom> noHouseStorageRoomList = this.storageRoomService.getNoHouseStorageRoomListByContractId(contract.getId());
        List<Integer> houseIdList = Lists.newArrayList();
        Map<String, StorageRoom> resultMap = Maps.newHashMap();
        if (CollectionUtils.isNotEmpty(houseList)) {
            for (House house : houseList) {
                houseIdList.add(house.getId());
            }
            resultMap = this.storageRoomService.getStorageRoomMap(houseIdList);
        }
        modelMap.addAttribute("contract", contract);
        modelMap.addAttribute("houseList", houseList);
        modelMap.addAttribute("houseTypeMap", houseTypeMap);
        modelMap.addAttribute("houseHoldMap", houseHoldMap);
        modelMap.addAttribute("resultMap", resultMap);
        modelMap.addAttribute("noHouseStorageRoomList", noHouseStorageRoomList);
    }

    /**
     * 选房保存
     *
     * @param request
     * @return
     */
    @RequestMapping("chooseHouseSave")
    @ResponseBody
    @RequiresPermissions("chooseHouse:chooseHouseSave")
    public Response chooseHouseSave(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Response response = this.houseService.chooseHouseSave(request);
        return response;
    }

    /**
     * 选房撤销
     *
     * @param request
     * @return
     */
    @RequestMapping("houseDelete")
    @ResponseBody
    @RequiresPermissions("chooseHouse:houseDelete")
    public Response houseDelete(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Response response = this.houseService.houseDelete(request);
        return response;
    }

    /***********************************************************储物间****************************************************************/

    /**
     * 选择储藏室列表
     */
    @RequestMapping("storageRoomList")
    @RequiresPermissions("chooseHouse:storageRoomList")
    public void storageRoomList(HttpServletRequest request, ModelMap modelMap) {
        Integer type = ParamUtils.getInt(request, "type", 0);
        Integer houseId = ParamUtils.getInt(request, "houseId", 0);
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        if (type == 2) {
            House house = this.houseService.findOne(houseId);
            Precondition.checkSqdsArguement(house != null && house.getAzProject() != null, "数据错误！未找到安置项目!");
            modelMap.addAttribute("house", house);
            modelMap.addAttribute("azProject", house.getAzProject());
            modelMap.addAttribute("contract", house.getContract());
        } else if (type == 1) {
            Contract contract = this.contractService.findOne(contractId);
            AzProject azProject = this.azProjectService.findOne(azProjectId);
            Precondition.checkSqdsArguement(contract != null && azProject != null, "数据错误！未找到安置项目!");
            modelMap.addAttribute("azProject", azProject);
            modelMap.addAttribute("contract", contract);
        }

    }

    /**
     * 加载储藏室信息数据
     *
     * @return
     */
    @RequestMapping("loadStorageRoomList")
    @ResponseBody
    @RequiresPermissions("chooseHouse:loadStorageRoomList")
    public Map loadStorageRoomList(HttpServletRequest request) {
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        List<Map> listData = Lists.newArrayList();
        PageBean<StorageRoom> storageRoomPageBean = this.storageRoomService.storageRoomListByProjectId(azProjectId);
        List<StorageRoom> storageRoomList = storageRoomPageBean.getDatas();
        Map<String, DataDict> houseStatusMap = this.dataDictService.getMapByParentNameForValue("房源状态");
        if (!com.app.system.utils.CollectionUtils.isEmpty(storageRoomList)) {
            Map<String, Object> item = null;
            Integer houseStatus = null;
            for (StorageRoom storageRoom : storageRoomList) {
                item = null;
                houseStatus = null;
                item = Maps.newHashMap();
                item.put("id", storageRoom.getId());
                item.put("buildNum", StringUtils.getNotNull(storageRoom.getBuildNum()));
                item.put("houseNum", storageRoom.getHouseNumStr());
                item.put("floor", IntegerUtils.getNotNull(storageRoom.getFloor()));
                item.put("buildArea", BigDecimalUtils.getNotNullStr(storageRoom.getBuildArea()));
                item.put("houseStatus", storageRoom.getHouseStatus());
                //如果是预留的，则显示为已选状态
                houseStatus = storageRoom.getHouseStatus().equals(HouseStatus.YULIU.getIndex()) ? HouseStatus.YIXUAN.getIndex() : storageRoom.getHouseStatus();
                item.put("houseStatusStr", StringUtils.dataDictShow(houseStatusMap.get(houseStatus.toString())));
                item.put("chooseHouseDate", DateUtils.dateToStringDefault(storageRoom.getChooseHouseDate()));
                item.put("memo", storageRoom.getMemo());
                listData.add(item);
            }
        }
        return LayUiUtils.page(storageRoomPageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 储藏室保存
     *
     * @param request
     * @return
     */
    @RequestMapping("storageRoomSave")
    @ResponseBody
    @RequiresPermissions("chooseHouse:storageRoomSave")
    public Response storageRoomSave(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Response response = this.storageRoomService.storageRoomSave(request);
        return response;
    }

    /**
     * 储藏室撤销
     *
     * @param request
     * @return
     */
    @RequestMapping("storageRoomDelete")
    @ResponseBody
    @RequiresPermissions("chooseHouse:storageRoomDelete")
    public Response storageRoomDelete(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Response response = this.storageRoomService.storageRoomDelete(request);
        return response;
    }

    /***********************************************************车位****************************************************************/


    /**
     * 加载车位数据
     *
     * @return
     */
    @RequestMapping("loadSelectCarLotInfo")
    @ResponseBody
    @RequiresPermissions("chooseHouse:loadSelectCarLotInfo")
    public Map loadSelectCarLotInfo(HttpServletRequest request) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        List<Map> resultData = Lists.newLinkedList();
        List<CarLot> carLotList = this.carLotService.getCarLotListByContractId(contract.getId());
        Map<String, DataDict> carLotTypeMap = this.dataDictService.getMapByParentNameForValue("车位类型");
        if (CollectionUtils.isNotEmpty(carLotList)) {
            Map<String, Object> mapItem = null;
            for (CarLot carLot : carLotList) {
                mapItem = null;
                mapItem = Maps.newHashMap();
                mapItem.put("id", carLot.getId());
                mapItem.put("projectName", carLot.getAzProject().getProjectName());
                mapItem.put("carLotInfo", carLot.getHouseNumStr() + "【" + carLot.getFloor() + "层】");
                mapItem.put("buildArea", carLot.getBuildArea());
                mapItem.put("carLotTypeStr", StringUtils.dataDictShow(carLotTypeMap.get(carLot.getCarLotType().toString())));
                resultData.add(mapItem);
            }
        }
        return LayUiUtils.page(0, resultData);
    }

    /**
     * 选择车位列表
     */
    @RequestMapping("carLotList")
    @RequiresPermissions("chooseHouse:carLotList")
    public void carLotList(HttpServletRequest request, ModelMap modelMap) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkSqdsArguement(contract != null && contract.getZsProject() != null, "数据错误！未找到征收项目!");
        modelMap.addAttribute("contract", contract);
    }

    /**
     * 加载车位数据
     *
     * @return
     */
    @RequestMapping("loadCarLotList")
    @ResponseBody
    @RequiresPermissions("chooseHouse:loadCarLotList")
    public Map loadCarLotList(HttpServletRequest request) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        List<Map> resultData = Lists.newArrayList();
        //得到该征收项目所分配的安置项目所有的车位
        PageBean<CarLot> pageBean = this.carLotService.getCarLotPageByZsProjectId(contract.getZsProject().getId());

        Map<String, DataDict> houseStatusMap = this.dataDictService.getMapByParentNameForValue("房源状态");
        Map<String, DataDict> carLotTypeMap = this.dataDictService.getMapByParentNameForValue("车位类型");
        if (CollectionUtils.isNotEmpty(pageBean.getDatas())) {
            Map<String, Object> mapItem = null;
            Integer houseStatus = null;
            for (CarLot carLot : pageBean.getDatas()) {
                mapItem = null;
                houseStatus = null;
                mapItem = Maps.newHashMap();
                mapItem.put("id", carLot.getId());
                mapItem.put("projectName", carLot.getAzProject().getProjectName());
                mapItem.put("buildNum", carLot.getBuildNum());
                mapItem.put("houseNum", carLot.getHouseNumStr());
                mapItem.put("floor", carLot.getFloor());
                mapItem.put("buildArea", carLot.getBuildArea());
                mapItem.put("carLotTypeStr", StringUtils.dataDictShow(carLotTypeMap.get(carLot.getCarLotType().toString())));
                mapItem.put("houseStatus", carLot.getHouseStatus());
                //如果是预留的，则显示为已选状态
                houseStatus = carLot.getHouseStatus().equals(HouseStatus.YULIU.getIndex()) ? HouseStatus.YIXUAN.getIndex() : carLot.getHouseStatus();
                mapItem.put("houseStatusStr", StringUtils.dataDictShow(houseStatusMap.get(houseStatus.toString())));
                resultData.add(mapItem);
            }
        }
        return LayUiUtils.page(pageBean.getTotalCount().intValue(), resultData);
    }

    /**
     * 车位保存
     *
     * @param request
     * @return
     */
    @RequestMapping("carLotSave")
    @ResponseBody
    @RequiresPermissions("chooseHouse:carLotSave")
    public Response carLotSave(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Response response = this.carLotService.carLotSave(request);
        return response;
    }

    /**
     * 车位撤销
     *
     * @param request
     * @return
     */
    @RequestMapping("carLotDelete")
    @ResponseBody
    @RequiresPermissions("chooseHouse:carLotDelete")
    public Response carLotDelete(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Response response = this.carLotService.carLotDelete(request);
        return response;
    }
    /***********************************************************店铺****************************************************************/

    /**
     * 已选店铺房源信息
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("selectShopHouseInfo")
    @RequiresPermissions("chooseHouse:selectShopHouseInfo")
    public void selectShopHouseInfo(HttpServletRequest request, ModelMap modelMap) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkSqdsArguement(contract != null, "数据错误，请重试！");
        List<ShopHouse> shopHouseList = this.shopHouseService.getShopHouseListByContractId(contract.getId());

        modelMap.addAttribute("contract", contract);
        modelMap.addAttribute("shopHouseList", shopHouseList);

    }

    /**
     * 选择店铺列表
     */
    @RequestMapping("shopHouseList")
    @RequiresPermissions("chooseHouse:shopHouseList")
    public void shopHouseList(HttpServletRequest request, ModelMap modelMap) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkSqdsArguement(contract != null && contract.getZsProject() != null, "数据错误！未找到征收项目!");
        modelMap.addAttribute("contract", contract);
    }

    /**
     * 加载店铺数据
     *
     * @return
     */
    @RequestMapping("loadShopHouseList")
    @ResponseBody
    @RequiresPermissions("chooseHouse:loadShopHouseList")
    public Map loadShopHouseList(HttpServletRequest request) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        List<Map> resultData = Lists.newArrayList();
        //得到该征收项目所分配的安置项目所有的车位
        PageBean<ShopHouse> pageBean = this.shopHouseService.getShopHousePageByZsProjectId(contract.getZsProject().getId());

        Map<String, DataDict> houseStatusMap = this.dataDictService.getMapByParentNameForValue("房源状态");
        if (CollectionUtils.isNotEmpty(pageBean.getDatas())) {
            Map<String, Object> mapItem = null;
            Integer houseStatus = null;
            for (ShopHouse shopHouse : pageBean.getDatas()) {
                mapItem = null;
                houseStatus = null;
                mapItem = Maps.newHashMap();
                mapItem.put("id", shopHouse.getId());
                mapItem.put("projectName", shopHouse.getAzProject().getProjectName());
                mapItem.put("buildNum", shopHouse.getBuildNum());
                mapItem.put("houseNum", shopHouse.getHouseNumStr());
                mapItem.put("floor", shopHouse.getFloor());
                mapItem.put("buildArea", shopHouse.getBuildArea());
                mapItem.put("poolArea", shopHouse.getPoolArea());
                mapItem.put("houseStatus", shopHouse.getHouseStatus());
                mapItem.put("szZhArea", shopHouse.getSxZhArea());
                //如果是预留的，则显示为已选状态
                houseStatus = shopHouse.getHouseStatus().equals(HouseStatus.YULIU.getIndex()) ? HouseStatus.YIXUAN.getIndex() : shopHouse.getHouseStatus();
                mapItem.put("houseStatusStr", StringUtils.dataDictShow(houseStatusMap.get(houseStatus.toString())));
                resultData.add(mapItem);
            }
        }
        return LayUiUtils.page(pageBean.getTotalCount().intValue(), resultData);
    }

    /**
     * 店铺保存
     *
     * @param request
     * @return
     */
    @RequestMapping("shopHouseSave")
    @ResponseBody
    @RequiresPermissions("chooseHouse:shopHouseSave")
    public Response shopHouseSave(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Response response = this.shopHouseService.shopHouseSave(request);
        return response;
    }

    /**
     * 店铺撤销
     *
     * @param request
     * @return
     */
    @RequestMapping("shopHouseDelete")
    @ResponseBody
    @RequiresPermissions("chooseHouse:shopHouseDelete")
    public Response shopHouseDelete(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Response response = this.shopHouseService.shopHouseDelete(request);
        return response;
    }


    /**
     * 打印房源签认表
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("chooseResultPrint")
    @RequiresPermissions("chooseHouse:chooseResultPrint")
    public void chooseResultPrint(HttpServletRequest request, ModelMap modelMap) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkSqdsArguement(contract != null && contract.getZsProject() != null, "数据错误！未找到协议信息!");
        modelMap.addAttribute("contract", contract);
    }

    /**
     * 打印房源预选单
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("chooseResultYxPrint")
    public void chooseResultYxPrint(HttpServletRequest request, ModelMap modelMap) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkSqdsArguement(contract != null && contract.getZsProject() != null, "数据错误！未找到协议信息!");
        modelMap.addAttribute("contract", contract);
    }


    /**
     * 查看所有房源、储物间、车位选择情况
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("viewChoose")
    @RequiresPermissions("chooseHouse:viewChoose")
    public void viewChoose(HttpServletRequest request, ModelMap modelMap) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkSqdsArguement(contract != null, "数据错误！未找到协议信息!");
        modelMap.addAttribute("contract", contract);
    }

    /**
     * 设置房源签认状态（可撤销）
     *
     * @param request
     */
    @RequestMapping("chooseHouseStatus")
    @ResponseBody
    @RequiresPermissions("chooseHouse:chooseHouseStatus")
    public synchronized Response chooseHouseStatus(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkAjaxArguement(contract != null, "1111", "数据错误！");

        if (contract.getChooseHouseDate() == null && StringUtils.isNotEmpty(contract.getBaoZhangType())) {
            this.houseService.setHouselzNum(contract);
        }
        if (contract.getZsProject().getId() == 146 && contract.getChooseHouseDate() == null) {
            List<House> houseList = this.houseService.getHouseListByContractId(contract.getId());
            Boolean isSet = false;
            if (CollectionUtils.isNotEmpty(houseList)) {
                for (House house : houseList) {
                    if (house.getHouseHold().equals("0100")) {
                        isSet = true;
                    }
                    if (house.getHouseHold().equals("1000") || house.getHouseHold().equals("0502")) {
                        isSet = false;
                        break;
                    }
                }
            }
            if (isSet) {
                Parameter parameter = this.parameterService.findOne(47);
                contract.setParameter(parameter);
            }
        }
        if (contract.getChooseHouseStatus().equals(Status.NO.getIndex())) {
            Precondition.checkAjaxArguement(contract.getLimitStatus() != null && contract.getLimitStatus().equals(Status.NO.getIndex()), "4444", "该家庭当前暂不能选房！");
            contract.setChooseHouseStatus(Status.YES.getIndex());
            contract.setChooseHouseDate(contract.getChooseHouseDate() == null ? new Date() : contract.getChooseHouseDate());

            this.actionHistoryService.saveActionHistoryByContractId(contract.getId(), "设置为已签认状态", "签认状态");

        } else {
            Precondition.checkAjaxArguement(contract.getFinanceCheckStatus().equals(Status.NO.getIndex()) || contract.getFinanceCheckStatus().equals(Status.TUI.getIndex()), "2222", "该家庭财务已复核通过，不允许撤销！");
            Precondition.checkAjaxArguement(contract.getAzSignStatus().equals(Status.NO.getIndex()) || contract.getAzSignStatus().equals(Status.TUI.getIndex()), "2222", "该家庭已签订协议，不允许撤销！");
            contract.setChooseHouseStatus(Status.NO.getIndex());
            this.actionHistoryService.saveActionHistoryByContractId(contract.getId(), "设置为未签认状态", "签认状态");

        }

        this.contractService.save(contract);
        return new Response("0001", "操作成功");
    }

    /**
     * 修改是否结算状态【关键节点面积更改】
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("areaJieSuanForm")
    @RequiresPermissions("chooseHouse:areaJieSuanForm")
    public void areaJieSuanForm(HttpServletRequest request, ModelMap modelMap) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkSqdsArguement(contract != null, "该户协议不存在");

        //得到对应的节点优惠面积
        Parameter parameter = this.parameterService.findOne(contract.getParameter().getId());
        BigDecimal overArea = this.parameterService.getOverArea(parameter.getId());
        modelMap.addAttribute("overArea",overArea);

        //超出面积即overArea,按市场价结算；当正常扩购时，Parameter的id默认是1，否则新建一个配置表，新建两条扩购数据
/*
        String overArea = ParamUtils.getString(request,"overArea","0");
*/




        modelMap.addAttribute("contract", contract);
        //房源状态
        List<DataDict> dataDictList = this.dataDictService.findListByParentName("是否");
        modelMap.addAttribute("dataDictList", dataDictList);
    }

    /**
     * 剩余房源结算状态保存
     *
     * @param request
     * @return
     */
    @ResponseBody
    @Transactional
    @RequestMapping("saveIsJieSuanStatus")
    @RequiresPermissions("chooseHouse:saveIsJieSuanStatus")
    public Response saveHouseStatus(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        //Response response = this.contractService.setIsJiSuanStatus(request);
        Response response = this.parameterService.setSaveOverArea(request);
        return response;
    }
}
